package com.test.shop.service.impl;

import com.test.shop.dao.UserDAO;
import com.test.shop.dao.jdbc.UserDAOImpl;
import com.test.shop.pojo.ManageResult;
import com.test.shop.pojo.User;
import com.test.shop.service.UserService;
import com.test.shop.util.Md5Util;
import com.test.shop.util.ShopConstant;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 徒有琴
 */
public class UserServiceImpl implements UserService {

    private UserDAO userDAO = new UserDAOImpl();

    @Override
    public ManageResult doRegist(String mobile, String password) {
        ManageResult result = new ManageResult();
        User user = userDAO.getUserByMobile(mobile);
        if (user != null) {
            result.setSuccess(false);
            result.setMessage("用户名已存在");
            return result;
        }
        password = Md5Util.getMD5Str(password + ShopConstant.PASSWORD_SALT);
        userDAO.addUser(mobile, password);
        result.setSuccess(true);
        return result;
    }

    @Override
    public ManageResult doLogin(String mobile, String password) {
        ManageResult result = new ManageResult();
        User user = userDAO.getUserByMobile(mobile);
        //password是null
        if (user == null
                || !user.getPassword().equals(Md5Util.getMD5Str(password + ShopConstant.PASSWORD_SALT))) {
            result.setSuccess(false);
            result.setMessage("用户名或密码错误");
            return result;
        }
//        password = Md5Util.getMD5Str(password + ShopConstant.PASSWORD_SALT);
//        if (!password.equals(user.getPassword())) {
//            result.setSuccess(false);
//            result.setMessage("用户名或密码错误");
//            return result;
//        }
        result.setSuccess(true);
        result.setOther(user);
        return result;
    }
}
